﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeGenerator
{
    public static class Common
    {
        public static class IO
        {
            public static void WriteTextFile(string file, string text)
            {
                using (StreamWriter outfile = new StreamWriter(file))
                {
                    outfile.Write(text);
                }

            }
            public static string GetApplicationPath()
            {
                FileInfo res = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

                return res.DirectoryName + "\\";
                //if (!res.EndsWith("\\"))
                //    res += "\\"; 
            }
        }

        public static class Templating
        {
            public static string GetTemplatesPath()
            {
                return IO.GetApplicationPath() + "\\Templates\\";
            }
            
            public static string GetExternalReferencesPath()
            {
                return IO.GetApplicationPath() + "\\Templates\\ExternalReferences\\";
            }

            public static string GetTemplatePlainText(string TemplateFile)
            {
                if (!File.Exists(Common.Templating.GetTemplatesPath() + TemplateFile))
                    throw new FileNotFoundException("Template file " + Common.Templating.GetTemplatesPath() + TemplateFile + " not found!");

                StreamReader myFile = new StreamReader(Common.Templating.GetTemplatesPath() + TemplateFile);
                string myString = myFile.ReadToEnd();
                myFile.Close();
                return myString;
            }
        }

        public static class Formatting
        {
            /// <summary>
            /// Convert the string to Pascal case.
            /// </summary>
            /// <param name="str">the string to turn into Pascal case</param>
            /// <returns>a string formatted as Pascal case</returns>
            public static string FormatPascalCase(string str)
            {
                StringBuilder sb = new StringBuilder(str.Length);

                if (string.IsNullOrEmpty(str))
                    throw new ArgumentException("A null or empty value cannot be converted", str);

                if (str.Length < 2)
                    return str.ToUpper();

                // Split the string into words.
                string[] words = str.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string word in words)
                    sb.Append(string.Format("{0}{1}", word.Substring(0, 1).ToUpper(), word.Substring(1)));

                return sb.ToString();
            }

            public static string ToString_OneTwoThree(string str)
            {
                StringBuilder sb = new StringBuilder(str.Length);

                if (string.IsNullOrEmpty(str))
                    throw new ArgumentException("A null or empty value cannot be converted", str);

                String[] words = str.Split('_');

                foreach (string word in words)
                {
                    if (word.Length < 2)
                    {
                        sb.Append(word.ToUpper());
                        break;
                    }
                    
                    else
                    {
                        sb.Append(String.Format("{0}{1}", word.Substring(0, 1).ToUpper(), word.Substring(1).ToLower()));
                    }
                }
                return sb.ToString();
            }
        }
    }
}

